﻿package lobby
{
	import flash.display.Shape;
	import flash.geom.Matrix;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.InterpolationMethod;
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * ...
	 * @author Tom
	 */
	
	public class Captcha extends MovieClip
	{
		public static const CAPTCHA_LENGTH:int = 5;
		public static const MARGINS:int = 9;
		public static const SPACE:int = 4;
		private var captcha_array:Array = new Array();
		private var captcha:String = "";
		private var ww:Number, hh:Number;
		
		public function Captcha(ww:Number, hh:Number):void
		{
			this.ww = ww;
			this.hh = hh;
		}
		
		public function initCaptcha():void
		{
			removeChildren();
			captcha_array = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
			captcha = "";
			//createBackgroundCapt();
			createCaptcha();
			createTextCapt();
		}
		
		private function removeChildren():void
		{
			var num:int = this.numChildren;
			for (var i:int = 0; i < num; i++)
			{
				this.removeChildAt(0);
			}
		}
		
		public function createBackgroundCapt():void
		{
			var type:String = GradientType.RADIAL;
			var colors:Array = [0xFFFFFF, 0xFFFFFF];
			var alphas:Array = [100, 100];
			var ratios:Array = [0xFF, 0xFF];
			var spreadMethod:String = SpreadMethod.REFLECT;
			var interpolationMethod:String = InterpolationMethod.LINEAR_RGB;
			var focalPointRatio:Number = 0.9;
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(100, 100, Math.PI, 0, 0);
			var square:Shape = new Shape();
			square.graphics.beginGradientFill(type, colors, alphas, ratios, matrix, spreadMethod, interpolationMethod, focalPointRatio);
			square.graphics.moveTo(0, 0);
			square.graphics.lineTo(ww, 0);
			square.graphics.lineTo(ww, hh);
			square.graphics.lineTo(0, hh);
			square.graphics.lineTo(0, 0);
			square.graphics.endFill();
			this.addChild(square);
		}
		
		private function createCaptcha():void
		{
			for (var i:int = 0; i < CAPTCHA_LENGTH; i++)
			{
				var randomNumber:Number = Math.floor(Math.random() * captcha_array.length);
				captcha += captcha_array[randomNumber];
			}
		}
		
		private function createTextCapt():void
		{
			for (var i:int = 0; i < captcha.length; i++)
			{
				var tmp:Number = Math.round(Math.random());
				if (tmp == 0)
				{
					tmp = 1;
				}
				else
				{
					tmp = -1;
				}
				var tf:TextField = new TextField();
				tf.autoSize = TextFieldAutoSize.LEFT;
				tf.multiline = false;
				tf.selectable = false;
				tf.text = captcha.substr(i, 1);
				var my_fmt:TextFormat = new TextFormat();
				my_fmt.font = "Tahoma";
				my_fmt.size = 22;
				my_fmt.color = 0x000000; //Math.random() * 0xDDDDDD;
				my_fmt.bold = true;
				tf.setTextFormat(my_fmt);
				this.addChild(tf);
				var tmpdis:Number = Math.round(Math.random() * tf.textHeight / 2);
				tf.x = MARGINS + i * (tf.textWidth + SPACE);
				tf.y = (hh - tf.textHeight) / 2 + tmp * tmpdis;
			}
			//trace("CAPTCHA:::" + captcha);
		}
		
		public function checkCaptcha(str:String):Boolean
		{
			if (str === captcha)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}